$(function () {
    $('#datetimepicker10').datetimepicker({
        //viewMode: 'years',
        format: 'YYYY-MM-DD'
    });
    $('#biao').bootstrapTable({
        url: '${pageContext.request.contextPath}/../../lin/query2',
        columns: [
            {
                checkbox : true
            },{
                field: 'id',
                title: 'id',
                visible : false,
            },
            {
                field: 'number',
                title: '货品编号',
                visible : false,
            },
            {
                field: 'name',
                title: '货品名称'
            }, {
                field: 'comptype.name',
                title: '单位'
            }, {
                field: 'spec',
                title: '规格型号'
            }, {
                field: 'colortype.name',
                title: '颜色'
            }, {
                field: 'bprice',
                title: '单价'
            }, {
                field: 'sum',
                title: '数量'
            }, {
                field: 'xx',
                title: '总金额',
                formatter: function (value, row, index) {

                    var jia = row.bprice * row.sum;
                    return jia;

                }
            }
        ],
        height: 450,
        striped:true,
        onDblClickRow: function (row, $element, field) {

            $("#myModal2").modal("show");
            $("#hpbh").text(row.number);
            $("#ggxh").text(row.spec);
            $("#lb").text("");
            $("#dqkc").text("");

            $("#hpmc").text(row.name);
            $("#dw").text(row.comptype.name);
            $("#ys").text(row.colortype.name);
            $("#ckjg").text(row.bprice+"元");

            $("#scjj").text("");
            $("#ckjj").val(row.bprice);
            $("#sum").val(row.sum);
            $("#ddd").attr("onclick","ti2()")
        },
    });

    $("#lin1").bootstrapTable({
        url: '${pageContext.request.contextPath}/../../lin/query1',
        columns: [
            {
                field: 'id',
                title: 'id',
                visible : false,
            },
            {
                field: 'name',
                title: '货品名称'
            }, {
                field: 'comptype.name',
                title: '单位'
            }, {
                field: 'spec',
                title: '规格型号'
            }, {
                field: 'colortype.name',
                title: '颜色'
            }, {
                field: 'bprice',
                title: '单价'
            }, {
                field: 'sum',
                title: '数量'
            }, {
                field: 'xx',
                title: '总金额',
                formatter: function (value, row, index) {
                    var jia = row.bprice * row.sum;
                    return jia;
                }
            }
        ],
        height: 600,
        striped:true,
    })


    $('#jiu').bootstrapTable({
            url: '${pageContext.request.contextPath}/../../commodity/query',
            method: "post",
            striped:true,
            contentType: "application/x-www-form-urlencoded",
            onDblClickRow: function (row, $element, field) {
                $("#myModal2").modal("show");
                $("#hpbh").text(row.number);
                $("#ggxh").text(row.spec);
                $("#lb").text(row.commtype.name);
                $("#dqkc").text(row.stock+""+ row.comptype.name);

                $("#hpmc").text(row.name);
                $("#dw").text(row.comptype.name);
                $("#ys").text(row.colortype.name);
                $("#ckjg").text(row.eprice+"元");

                $("#scjj").text(row.bprice);
                $("#ckjj").val(row.bprice);
                $("#sum").val("1");

                $("#lei1").val(row.commtype.id);
                $("#yan1").val(row.colortype.id);
                $("#dan1").val(row.comptype.id);
                $("#ddd").attr("onclick","ti1()")
            },
            columns: [{
                field: 'id',
                title: 'id',
                visible : false,
            },{
                field: 'number',
                title: '产品编号'
            }, {
                field: 'name',
                title: '产品名称',
            },{
                field: 'commtype.id',
                title: '类别id',
                visible : false,
            }, {
                field: 'commtype.name',
                title: '类别名',
                visible : false,
            },{
                field: 'comptype.id',
                title: '单位id',
                visible : false,
            },{
                field: 'comptype.name',
                title: '单位',
                // formatter : function(value, row, index) {
                //     if(row.sex==1){
                //         return "男";
                //     }else if(row.sex==2){
                //         return "女";
                //     }
                // }
            }, {
                field: 'spec',
                title: '规格'
            }, {
                field: 'colortype.id',
                title: '颜色id',
                visible : false,
            },{
                field: 'colortype.name',
                title: '颜色'
            }, {
                field: 'miniin',
                title: '最低库存',
                visible : false,
            },{
                field: 'bprice',
                title: '参考进价',
            },{
                field: 'eprice',
                title: '卖家',
                visible : false,
            }, {
                field: 'stock',
                title: '库存',
                //visible : false,
            },{
                field:'warehouse.name',
                title:'仓库',
            }],
            queryParamsType: '',
            //修改分页的默认对数
            //offset,limit
            queryParams: queryParams,
            /* //是否显示刷新按钮。
             showRefresh:true,
             //是否显示内容列下拉框。
             showColumns:true,*/
            height: 394,
            pageNumber: 1,
            pageSize: 9,
            pagination: true,
            sidePagination: 'server',
        })
});


function search() {

    var opt = $("#jiu").bootstrapTable('getOptions');

    var name = $("#name").val();
    var lei = $('#lei').combogrid('getValue');
    //alert(name) ;

    $.post('${pageContext.request.contextPath}/../../commodity/query', {
        'lei':lei,
        'name': name,
        'chang':null,
        'pageSize': opt.pageSize,
        'pageNumber': opt.pageNumber
    }, function (data) {

        //重新给table绑定数据
        $("#jiu").bootstrapTable("load", data);
    });
}

function queryParams(params) {
    var temp = {  //这里的键的名字和控制器的变量名必须一直，这边改动，控制器也需要改成一样的
        'pageSize': params.pageSize,  //页面大小
        'pageNumber': params.pageNumber, //页码
        'name': $("#name").val(),
        'lei' :$ ('#lei').combogrid('getValue'),
        'chang':null,

    };
    return temp;
};

function lao() {
    $("#myModal").modal("show");
}

function xin() {
    $("#myModal3").modal("show")

    $.post("${pageContext.request.contextPath}/../../commodity/c",function (data) {
        var number = data;
        $("#bian").val(number+1);
    })



}

function ti1() {

    var number = $("#hpbh").text();
    var name = $("#hpmc").text();
    var companyid = $("#dan1").val();
    var spec = $("#ggxh").text();
    var colorid = $("#yan1").val();
    var bprice = $("#ckjj").val();
    var sum = $("#sum").val();
    var type = "1";

    if(bprice==null || bprice==""){
        sweetAlert("请输入进货价格");
        return false;
    }
    if(sum==null || sum==""){
        sweetAlert("请输入进货数量");
        return false;
    }
    $.post("${pageContext.request.contextPath}/../../lin/add",{"number":number,"name":name,"companyid":companyid, "spec":spec,"colorid":colorid,"bprice":bprice,"sum":sum,"type":type},function (data) {
        $("#lin1").bootstrapTable("load",data);
    })

    $("#myModal2").modal("hide")
}

function  ti2() {
    var number = $("#hpbh").text();
    var bprice = $("#ckjj").val();
    var sum = $("#sum").val();

    if(bprice==null || bprice==""){
        sweetAlert("请输入进货价格");
        return false;
    }
    if(sum==null || sum==""){
        sweetAlert("请输入进货数量");
        return false;
    }
    $.post("${pageContext.request.contextPath}/../../lin/updsum",{"number":number,"bprice":bprice,"sum":sum},function (data) {
        $("#biao").bootstrapTable("load",data);
    })
    $("#myModal2").modal("hide")


    var timeout=setTimeout(function () {
        var arr1 = $("#biao").bootstrapTable('getData');
        var zhong = 0;
        $.each(arr1,function(i,e){
            var sum = arr1[i].sum;
            var qian = arr1[i].bprice;
            zhong+=sum*qian;
        })

        $("#zhong").text(zhong);
    }, 100);
}

// 下拉列表添加
function tianlei() {
    $("#myModal4").modal("show");
    $("#t").val('');
    $("#tian").text("类别：");
    $("#t").attr("placeholder","请输入新类别")
    $("#t1").attr("onclick","tianlei1()");
}
function tianlei1() {
    var t = $("#t").val();
    if(t==null || t==''){
        sweetAlert("请输入类别");
        return false;
    }

    $.post("${pageContext.request.contextPath}/../../commtype/add",{"name":t},function (data) {
        $("#xuan").empty();
        $("#xuan").append('<option value=-1>请选择 </option>')
        var e=data;
        $(e).each(function (i,e) {
            $('#xuan').append('<option value='+e.id+' >'+e.name+'</option>');
        })
    })

}

function tiandan() {
    $("#myModal4").modal("show");
    $("#t").val('');
    $("#tian").text("单位：");
    $("#t").attr("placeholder","请输入新单位")
    $("#t1").attr("onclick","tiandan1()");
}
function tiandan1() {
    var t = $("#t").val();
    if(t==null || t==''){
        sweetAlert("请输入单位");
        return false;
    }

    $.post("${pageContext.request.contextPath}/../../comp/add",{"name":t},function (data) {
        $("#dw1").empty();
        $("#dw1").append('<option value=-1>请选择 </option>')
        var e=data;
        $(e).each(function (i,e) {
            $('#dw1').append('<option value='+e.id+' >'+e.name+'</option>');
        })
    })
}

function tianyan() {
    $("#myModal4").modal("show");
    $("#t").val('');
    $("#tian").text("颜色：");
    $("#t").attr("placeholder","请输入新颜色")
    $("#t1").attr("onclick","tianyan1()");
}
function tianyan1() {
    var t = $("#t").val();
    if(t==null || t==''){
        sweetAlert("请输入新颜色");
        return false;
    }

    $.post("${pageContext.request.contextPath}/../../color/add",{"name":t},function (data) {
        $("#ys1").empty();
        $("#ys1").append('<option value=-1>请选择 </option>')
        var e=data;
        $(e).each(function (i,e) {
            $('#ys1').append('<option value='+e.id+' >'+e.name+'</option>');
        })
    })

}

function add2() {
    var lei = $("#xuan").val();
    var bian = $("#bian").val();
    var xinhao = $("#xh").val();
    var danwei = $("#dw1").val();
    var money = $("#money").val();
    var color = $("#ys1").val();
    var name = $("#nname").val();
    var sum = $("#sum1").val();

    if(lei == -1){
        sweetAlert("请选择商品类别");
        return false;
    }else if(danwei == -1){
        sweetAlert("请选择商品单位");
        return false;
    }else if(color == -1){
        sweetAlert("请选择商品颜色");
        return false;
    }
    
    $.post("${pageContext.request.contextPath}/../../lin/add2",{"number":bian,"name":name,"companyid":danwei, "spec":xinhao,"colorid":color,"bprice":money,"sum":sum,"type":2},function (data) {
        $("#biao").bootstrapTable("load",data);
    })
}


function save(){
    var gong = $('#ghs').combogrid('getValue');
    var changku = $("#ck").combogrid('getValue');
    var tim = $("#time").val();
    var order = $("#order").text();
    var jingbanren = $("#jbr").combogrid('getValue');

    var zhong = $("#zhong").text();

    var tt = time();

    $.ajax({
        type : "post",
        url : "${pageContext.request.contextPath}/../../lin/chanumber",
        async : false,
        datatype : "json",
        success : function(data) {
            if(data==null || data==''){
                sweetAlert("单据中没有业务发生无法保存！");
                return false;
            }else{
                if(gong==null || gong==''){
                    sweetAlert("请选择供货商！");
                    return false;
                }else if(changku==null || changku==''){
                    sweetAlert("请选择仓库！");
                    return false;
                }else if(jingbanren==null || jingbanren==''){
                    sweetAlert("请选择经办人！");
                    return false;
                }

                if(tim.length == 10){
                    tt = tim;
                }
                var typeid = 1;

                
                $.post("${pageContext.request.contextPath}/../../j/add",{"order":order,"supplierid":gong,"date":tt,"money":zhong,"typeid":typeid,"agent":jingbanren,"warehouseid":changku},function (data) {
                    if(data!=0){

                        $.post("${pageContext.request.contextPath}/../../commodity/comprehensive",{"ckid":changku},function (data) {
                            $.post("${pageContext.request.contextPath}/../../lin/qing",function (data) {
                                $("#biao").bootstrapTable("load",data);
                                sweetAlert("提交成功")
                                Win10_child.newMsg('提示','已成功进货！');

                                Win10_child.close();


                            })
                        })



                    }else{
                    }
                })

            }
        }
    });






    


}
// 获取当前时间
function time(){
    var date = new Date();
    var seperator1 = "-";
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var strDate = date.getDate();
    if (month >= 1 && month <= 9) {
        month = "0" + month;
    }
    if (strDate >= 0 && strDate <= 9) {
        strDate = "0" + strDate;
    }
    var time = year + seperator1 + month + seperator1 + strDate;
    return time;
}



